<?php
/**
 * Created by PhpStorm.
 * User: xlosy
 * Date: 2018/3/21
 * Time: 23:25
 */

namespace app\admin\controller;

use app\index\model\Catalog;
use app\index\model\Decade;
use app\index\model\Film;
use app\index\model\Level;
use app\index\model\Loc;
use app\index\model\Res;
use app\index\model\Subclass;
use app\index\model\Type;
use app\index\model\Vipcode;

/**
 * 后台管理
 * Class Manager
 * @package app\Manager\controller
 */
class Manager extends Base
{
    public function index(){
        $this->assign('data',$_GET);
        return $this->redirect('manager/film_list');
    }


    public function head_meta(){
        return $this->fetch();
    }


    /**
     * 删除电影（逻辑删除）
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function delFilm(){
        $film = Film::get(input('film_id'));
        $film->isUse='0';
        $rtn = array();
        if($film->save()==1){
            $rtn['code'] = '1';
        }else{
            $rtn['code'] = '0';
        }
        return json($rtn);
    }

    /**
     * 目录
     */
    public function catalogs() {
        /**查询一级菜单*/
        $catalog = new Catalog();
        $return = $catalog->get_cata_log_list();
        foreach ($return as $key => $value){
           $subClass =  new Subclass();
           $value['subClassList'] = $subClass->get_sub_class_list($value->id);
        }
        /**传递数据到模板*/
        $this->assign('cataLogList',$return);
        /*dump($return);
        die;*/
        /**查询所有类型*/
        $type = new Type();
        $return = $type->get_type_list();
        foreach ($return as $key => $value){
            $subClass =  new Subclass();
            $value['subClass'] = $subClass->get_sub_class_list_by_id($value->subclass_id);
        }
        /*dump($return);
        die;*/
        /**传递数据到模板*/
        $this->assign('typeList',$return);

        /**查询所有地理位置*/
        $loc = new Loc();
        $return = $loc->get_loc_list();
        /**传递数据到模板*/
        $this->assign('locList',$return);

        /**查询所有等级*/
        $level = new Level();
        $return = $level->get_level_list();
        /**传递数据到模板*/
        $this->assign('levelList',$return);

        /**查询所有年代*/
        $decade = new Decade();
        $return = $decade->get_decade_list();
        /**传递数据到模板*/
        $this->assign('decadeList',$return);

        return $this->fetch();
    }

    /**
     * 获取二级菜单
     */
    public function getSubClass(){
        $catalog_id = input('catalog_id');
        $subClass =  new Subclass();
        $return = $subClass->get_sub_class_list($catalog_id);
        return json($return);
    }

    /**
     * 增加一级目录
     */
    public function addCataLog(){
        $catalog = new Catalog($_POST);
        $catalog->id = create_uuid();
        $catalog->isUse = '1';
        $catalog->allowField(true)->save();
        return json($catalog->id);
    }

    /**
     * 增加二级目录
     */
    public function addSubClass(){
        $subClass = new Subclass();
        $subClass->id = create_uuid();
        $subClass->isUse =  '1';
        $subClass->catalog_id = input('cataLog_id');
        $subClass->name = input('name');
        $subClass->save();
        return json($subClass->id);
    }

    /**
     * 增加类型
     */
    public function addType(){
        $type = new Type();
        $type->id = create_uuid();
        $type->isUse =  '1';
        $type->subclass_id = input('subClass_id');
        $type->name = input('name');
        $type->save();
        return json($type->id);
    }

    /**
     * 增加类型
     */
    public function getType(){
        $type = new Type();
        return json($type->get_type_list_by_subClass_id(input('subClass_id')));
    }

    /**
     * 增加地区
     */
    public function addLoc(){
        $loc = new Loc();
        $loc->id = create_uuid();
        $loc->isUse =  '1';
        $loc->name = input('name');
        $loc->save();
        return json($loc->id);
    }

    /**
     * 增加级别
     */
    public function addLevel(){
        $level = new Level();
        $level->id = create_uuid();
        $level->isUse =  '1';
        $level->name = input('name');
        $level->save();
        return json($level->id);
    }


    /**
     * 增加年代
     */
    public function addDecade(){
        $decade = new Decade();
        $decade->id = create_uuid();
        $decade->isUse =  '1';
        $decade->name = input('name');
        $decade->save();
        return json($decade->getLastSql());
    }

    /**
     * 管理影片
     */
    public function film_list(){

        $film = new Film();
        $return = $film->get_film_list(32);
        /**传递数据到模板*/
        $this->assign('filmList',$return);

        /**传递数据到模板*/
        $this->assign('name',input('name'));
        return $this->fetch();
    }


    /**
     * 管理修改、资源添加影片
     */
    public function film(){
        $film_id =  input("film_id");
        if ($film_id != null && ""!=$film_id) {
            $film = new Film();
            /**传递数据到模板*/
            $this->assign('film',$film->get_film_by_id($film_id));

            $res = new Res();
            $list = $res->find_res_by_film_id($film_id);

            if (count($list) == 0) {
                /**传递数据到模板*/
                $this->assign('res',null);
            } else {
                /**传递数据到模板*/
                $this->assign('res',$list);
            }
        }else{
            /**传递数据到模板*/
            $this->assign('film',null);
            /**传递数据到模板*/
            $this->assign('res',null);
        }

        /**查询一级菜单*/
        $catalog = new Catalog();
        $return = $catalog->get_cata_log_list();
        foreach ($return as $key => $value){
            $subClass =  new Subclass();
            $value['subClassList'] = $subClass->get_sub_class_list($value->id);
        }
        /**传递数据到模板*/
        $this->assign('cataLogList',$return);
        /**查询所有类型*/
        $type = new Type();
        $return = $type->get_type_list();
        foreach ($return as $key => $value){
            $subClass =  new Subclass();
            $value['subClass'] = $subClass->get_sub_class_list_by_id($value->subclass_id);
        }
        /**传递数据到模板*/
        $this->assign('typeList',$return);

        /**查询所有地理位置*/
        $loc = new Loc();
        $return = $loc->get_loc_list();
        /**传递数据到模板*/
        $this->assign('locList',$return);

        /**查询所有等级*/
        $level = new Level();
        $return = $level->get_level_list();
        /**传递数据到模板*/
        $this->assign('levelList',$return);

        /**查询所有年代*/
        $decade = new Decade();
        $return = $decade->get_decade_list();
        /**传递数据到模板*/
        $this->assign('decadeList',$return);
        return $this->fetch();
    }


    /**
     * 增加影片信息
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function addFilm(){
        $film = new Film($_POST);
        if(is_null(input('id'))){
            $film->id = create_uuid();
        }
        $film->isUse = '1';
        $film->evaluation  = '0';
        $film->updateTime = date('Y-m-d H:i:s');
        $type = new Type();
        $type = $type->get($film->type_id);

        $subclass = new Subclass();
        $subclass = $subclass->get($type->subclass_id);

        $catalog = new Catalog();
        $catalog = $catalog->get($subclass->catalog_id);


        $film->subClass_id =  $subclass->id;
        $film->subClassName = $subclass->name;
        $film->cataLog_id = $catalog->id;
        $film->cataLogName = $catalog-> name;

        $film->allowField(true)->save();
        return json($film->id);
    }

    /**
     * 增加影片资源
     */
    public function addRes(){
        $res = new Res($_POST);
        $res->id = create_uuid();
        $res->isUse = "1";
        $res->updateTime = date('Y-m-d H:i:s');

        /**
         * 多资源上传
         */
        $rtId=0;
        $resName = input('name');
        if (strpos($resName,'@@')!==false) {
            //多资源上传
            $resNameArray = explode('##',trim($resName," "));//xxxx@@集&集数开始&集数结束&分割符号
            $name = $resNameArray[0];
            $begin = intval($resNameArray[1]);
            $end = intval($resNameArray[2]);
            $flag = $resNameArray[3];
            $res_link_valArray = explode($flag,str_replace("\\n","",input('link')));
            $cz = $begin-1;
            for ($x=$begin; $x<=$end; $x++) {
                res.setName(str_replace("@@",$x+"",$name));
                res.setEpisodes($x);
                res.setLink($flag+$res_link_valArray[$x-$cz]);
                $res->save();
                $rtId = $res->id;
            }
        }else{
            /**
             * 添加地区
             */
            $res->allowField(true)->save();
            $rtId = $res->id;
        }

        /**最近更新时间*/
        $film = new Film();
        $film->get_film_by_id(input("film_id"));
        $film->updateTime = date('Y-m-d H:i:s');
        $film->save();

        return json($rtId);
    }

    /**
     * 删除资源
     */
    public function delRes(){
        if(!is_null(input('res_id'))){
            return json(Res::destroy([input('res_id')]));
        }else{
            return json(0);
        }
    }

    /**
     * 修改资源使用状态
     */
    public function updateIsUse(){
        $res = Res::get(input('res_id'));
        if($res->isUse == '1'){
            $res->isUse   = '0';
        }else{
            $res->isUse   = '1';
        }
        return $res->save();
    }

    /**
     * 文件上传
     */
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('download');
        $path = 'static'. DS . 'upload'.DS.input('childPath');
        // 移动到框架应用根目录/public/static/upload/ 目录下
        $info = $file->move(ROOT_PATH . 'public'.DS.$path);

        $retu = array();
        if($info){
            $retu['filePath'] = 'public'.DS.$path.DS.($info->getSaveName());
        }else{
            //上传失败获取错误信息
            $retu['filePath'] = '-1';
        }
        return json($retu);
    }

    /**
     * 文件删除
     */
    public function delete_file(){
        $file_path = input("picsPath");
        $return_array = array();
        if(!is_null($file_path)){
            $file_path = ROOT_PATH.$file_path;
            unlink ($file_path);
            $return_array['code'] = '1';
        }else{
            $return_array['code'] = '0';

        }
        return json($return_array);
    }

    /**
     * 修改影片信息
     */
    public function updateFilmInfo(){
        $key = input('key');
        $val = input('val');
        $film = new Film();
        $film =  $film->get_film_by_id(input('film_id'));
        $film->updateTime = date('Y-m-d H:i:s');
        switch ($key) {
            case "name":
                $film->name = $val;
                break;
            case "image":
                unlink (ROOT_PATH.($film->image));
                $film->image = $val;
                break;
            case "onDecade":
                $film->onDecade = $val;
                break;
            case "status":
                $film->status = $val;
                break;
            case "resolution":
                $film->resolution = $val;
                break;
            case "typeName":
                $film->typeName = $val;
                break;
            case "type_id":
                $film->type_id = $val;

                $type = new Type();
                $type = $type->get($film->type_id);

                $subclass = new Subclass();
                $subclass = $subclass->get($type->subclass_id);

                $catalog = new Catalog();
                $catalog = $catalog->get($subclass->catalog_id);

                $film->subClass_id =  $subclass->id;
                $film->subClassName = $subclass->name;
                $film->cataLog_id = $catalog->id;
                $film->cataLogName = $catalog-> name;
                break;
            case "actor":
                $film->actor = $val;
                break;
            case "locName":
                $film->locName = $val;
                break;
            case "loc_id":
                $film->loc_id = $val;
                break;
            case "plot":
                $film->plot = $val;
                break;
            case "isVip":
                $film->isVip = $val;
                break;
        }
        $isOK = $film->save();
        if ($isOK) {
            return "1";
        } else {
            return "0";
        }
    }


    /**
     * vip管理
     */
    public function vip_manager(){
       $vip_codes = new Vipcode();
       $vip_codes = $vip_codes->where(['is_use'=>'1'])->select();
       $this->assign("vip_codes",$vip_codes);
       return $this->fetch();
    }


    /**
     * vip管理
     */
    public function create_vip_code(){
        $num = input("num");
        $vip_codes = array();
        $vip_code_obj = new Vipcode();
        for ($i =0;$i<$num;$i++){
            $vip_code = array();
            $vip_code['id'] = create_uuid();
            $vip_code['is_use'] = "1";
            $vip_code['code'] = strtoupper(create_uuid());
            $vip_code['create_time']= date("Y-m-d H:i:s");
            $vip_code['expire_time']= date("Y-m-d H:i:s");
            array_push($vip_codes,$vip_code);
        }
        $rtn_obj = $vip_code_obj->saveAll($vip_codes,false);
        if($rtn_obj!=0){
            $rtn['code'] = '1';
            $rtn['data'] = $vip_codes;
        }else{
            $rtn['code'] = '0';
        }
        return json($rtn);
    }

}